﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StateGrid;
using StateGrid.JJBK;
using StateGrid.User;
using StateGrid.GlobalVariables;
using iTextSharp.text.pdf;
using StateGrid.OperationLog;

public partial class AddPM : System.Web.UI.Page
{
    /// <summary>
    /// 经济报刊操作对象
    /// </summary>
    private JJBK_DAO jjbkDAO = new JJBK_DAO();
    /// <summary>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!UserManageDAO.IsLogin((User)Session["login"]))
        {
            GlobalMethod.RedirectLogin(Response);
            return;
        }

        if (!this.IsPostBack)
        {
            this.BindData();
        }

    }

    /// <summary>
    /// 绑定初始化数据
    /// </summary>
    private void BindData()
    {
        
        String message = null;
        //获取分类编码
        String flbm = Request["flbm"];
        //年份
        String nf = Request["nf"];
        //期号
        String qh = Request["qh"];


        JJBK_FL jjbkFL = null;
        //验证年份
        if (null == nf || nf.Length != 4)
        {
            message = "错误：年份信息有误";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }

        //验证期号
        if (null == qh || qh.Length != 2)
        {
            message = "错误：期号信息有误";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }

        if (null == flbm || "".Equals(flbm))
        {
            message = "错误：分类编码为空";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }
        else
        {
            jjbkFL = jjbkDAO.GetJJBK_FLByFlbm(flbm);
            if (null == jjbkFL)
            {
                message = "错误：没有找到对应的分类编码";
                Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
                return;
            }
        }

        //设置返回按钮链接
        this.backHyperLink.NavigateUrl = GlobalJJBK.LISTPM_PAGE + "?catalog=" + flbm;

        JJBK_PM jjbkPM = this.jjbkDAO.getMaxJJBK_PM(flbm, nf, qh);
       
        //分类编码
        this.flbmTextBox.Text = jjbkFL.flbm;
        //分类名称
        this.flmcTextBox.Text = jjbkFL.flmc;
        //索引编码
        this.sybmTextBox.Text=flbm+'-'+nf+'-'+qh;
        //年份
        this.nfTextBox.Text = nf;
        //期号
        this.qhTextBox.Text = qh;

        //标题
        this.btTextBox.Text = "";
       
        //报告日期
        this.rqTextBox.Text = nfTextBox.Text+"-"+qhTextBox.Text;
        
        //栏目
        this.lmTextBox.Text = "";
        //作者
        this.zzTextBox.Text = "";

        //获取文件名信息,不包括后缀
        String wjm = null;
        if (null == jjbkPM)
        {
            wjm = GetWJM(null);
            
        }
        else
        {
            wjm = GetWJM(jjbkPM.wjm);
        }

        //文件名
        this.wjmTextBox.Text = wjm;
        User user=(User)Session["login"];
        if(null==user)
        {
            GlobalMethod.RedirectLogin(Response);
            return;
        }
        //登录人
        this.dlrTextBox.Text = user.name;
    }

    /// <summary>
    /// 获取用户填写的国研报告篇名信息
    /// </summary>
    /// <returns></returns>
    private JJBK_PM GetJJBKPM()
    {
        JJBK_PM jjbkPM = new JJBK_PM();
        //索引编码
        jjbkPM.sybm = this.sybmTextBox.Text;
        //分类编码
        jjbkPM.flbm = this.flbmTextBox.Text;
        //文件名
        jjbkPM.wjm = this.wjmTextBox.Text;
        //原文件名
        jjbkPM.ywjm = this.ywjmFileUpload.FileName;
        //标题
        jjbkPM.bt = this.btTextBox.Text.Trim();
        //作者
        jjbkPM.zz = this.zzTextBox.Text.Trim();
        //日期
        jjbkPM.rq = DateTime.Parse(this.rqTextBox.Text);
        //版号
        jjbkPM.bh = this.bhTextBox.Text.Trim();
        //栏目
        jjbkPM.lm = this.lmTextBox.Text.Trim();
        
        //年份
        jjbkPM.nf = Int32.Parse(this.nfTextBox.Text);
        //月份
        jjbkPM.yf = Int32.Parse(this.qhTextBox.Text);
        //更新日期
        jjbkPM.gxrq = DateTime.Now;
        //登录人
        jjbkPM.dlr = this.dlrTextBox.Text;
        //全文
        jjbkPM.qw = "-1";

        return jjbkPM;

    }

    /// <summary>
    /// 获取下一个文件名，不包括后缀
    /// </summary>
    /// <param name="wjm"></param>
    /// <returns></returns>
    private String GetWJM(String wjm)
    {
        String result = null;
        if (null == wjm || "".Equals(wjm))
        {
            result = this.sybmTextBox.Text + "-0000";
        }
        else
        {
            //获取文件名的后四位流水号，并在其基础上加1
            int endIndex = wjm.LastIndexOf(".");

            String phString = wjm.Substring(endIndex - 4, 4);

            int ph = 0;

            try
            {
                ph = Int32.Parse(phString);
            }
            catch (Exception)
            {
            }

            //当前篇号+1
            ph += 1;

            phString = ph.ToString();
            //长度为1
            if (phString.Length == 1)
            {
                phString = "000" + phString;
            }
            else if (phString.Length == 2)
            {
                phString = "00" + phString;
            }
            else if (phString.Length == 3)
            {
                phString = "0" + phString;
            }
            else if (phString.Length >= 3)
            {
                String message = "错误：篇名数量已经超过了10000篇";
                Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
                return null;
            }
            result = this.sybmTextBox.Text + "-" + phString ;
        }
        result += ".PDF";
        return result;
    }

    protected void submitButton_Click(object sender, EventArgs e)
    {
        if (!this.ValidateWJM())
        {
            return;
        }
        if (!this.ValidateRQ())
        {
            return;
        }
        if (!this.ValidateBT())
        {
            return;
        }
        if (!ValidateYWJMFileUpload())
        {
            return;
        }
        JJBK_PM jjbkPM = this.GetJJBKPM();
        if (OperationFile.Upload(this.ywjmFileUpload, jjbkPM.wjm))
        {
            if (this.jjbkDAO.InsertJJBK_PM(jjbkPM))
            {
                Response.Write("<Script>alert('添加经济报刊篇名信息成功')</Script>");
                this.BindData();
            }
        }
        else
        {
            Response.Write("<Script>alert('添加经济报刊篇名信息失败')</Script>");
        }
    }

    /// <summary>
    /// 验证文件名
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateWJM()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        JJBK_PM jjbkPM = this.jjbkDAO.GetJJBK_PMByWjm(this.wjmTextBox.Text);
        if (null != jjbkPM)
        {
            this.errorLabel.Text = "错误：该文件名已存在";
            flag = false;
        }
        return flag;
    }

    /// <summary>
    /// 验证日期
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateRQ()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        this.rqTextBox.Text = this.rqTextBox.Text.Trim();
        try
        {
            DateTime.Parse(this.rqTextBox.Text.Trim());
        }
        catch (Exception)
        {
            this.errorLabel.Text = "错误：报告日期不正确";
            flag = false;
        }
        return flag;
    }

    /// <summary>
    /// 验证标题
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateBT()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        if(null==this.btTextBox.Text||"".Equals(this.btTextBox.Text.Trim()))
        {
            this.errorLabel.Text = "错误：标题不能为空";
            flag = false;
        }
        return flag;
    }

    /// <summary>
    /// 验证上传文件
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateYWJMFileUpload()
    {
        this.errorLabel.Text = "";
        Boolean flag = true;
        if (null == this.ywjmFileUpload || !this.ywjmFileUpload.HasFile)
        {
            this.errorLabel.Text = "错误：请选择上传的文件";
            flag = false;
        }
        else
        {
            if (!this.ywjmFileUpload.FileName.EndsWith(".PDF", true, null))
            {
                this.errorLabel.Text = "错误：请选择PDF类型文件";
                flag = false;
            }
            else
            {
                PdfReader pdf = new PdfReader(this.ywjmFileUpload.FileBytes);
                if (null != pdf)
                {
                    //文件大小不能超过10M
                    if (pdf.FileLength >= 10 * 1024 * 1024)
                    {
                        this.errorLabel.Text = "错误：选择的文件大小超过了10M";
                        pdf.Close();
                        return false;
                    }

                    this.ysHiddenField.Value = pdf.NumberOfPages.ToString();
                }

                pdf.Close();
            }
        }
        return flag;
    }

    protected void zzTextBox_TextChanged(object sender, EventArgs e)
    {
        this.zzTextBox.Text = GlobalMethod.Semicolon(this.zzTextBox.Text.Trim());
    }
}